﻿using Ducu.SRV.ServiceInteface;
using Ducu.SRV.ViewModel.Profile;
using Ducu.UI.Responsible.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ducu.UI.Responsible.Controllers
{
    public class ProfileController : BaseController
    {
        #region Constructor

        private IProfileService _profileService;
        private ISharedService _shareService;
        public ProfileController(IProfileService profileService,
            ISharedService shareService)
        {
            _profileService = profileService;
            _shareService = shareService;
        }

        #endregion

        #region URL: /Profile/Edit

        [NeedLogOn]
        public ActionResult Edit()
        {
            EditModel model = _profileService.Get();
            if (string.IsNullOrEmpty(Prepage.GetFromUrl()))
            {
                ViewBag.Prepage = "/Plan/New";
            }
            else
            {
                ViewBag.Prepage = Server.UrlDecode(Prepage.GetFromUrl());
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            _profileService.Save(model);

            return Prepage.Return("/Plan/New");
        }

        #endregion

        #region Child Actions

        [ChildActionOnly]
        public PartialViewResult _Show(int userId)
        {
            ViewBag.CanEdit = _shareService.GetNavigator().UserId == userId;
            return PartialView(_profileService.Get(userId));
        }

        #endregion
    }
}